package lijunyan;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class DateJPanel extends JPanel implements ActionListener
{
    private ActionListener listener;
    private final JComboBox<Integer> comboBoxYear;
    private final JComboBox<Integer> comboBoxMonth;
    private final JComboBox<Integer> comboBoxDay;

    public DateJPanel()
    {
        this(new MyDate());
    }
    public DateJPanel(ActionListener listener)
    {
        this(new MyDate());
        this.listener=listener;
    }
    public DateJPanel(MyDate date,ActionListener listener)
    {
        this(date);
        this.listener=listener;
    }
    public DateJPanel(MyDate date)
    {
        super();
        this.listener=null;
        Integer[] years=new Integer[200];
        for(int i=0;i<years.length;years[i]=i+1900,++i) ;
        Integer[] months=new Integer[12];
        for(int i=0;i<months.length;months[i]=i+1,++i) ;

        this.setLayout(new FlowLayout());
        this.add(this.comboBoxYear=new JComboBox<Integer>(years));
        this.add(new JLabel("年"));
        this.add(this.comboBoxMonth=new JComboBox<Integer>(months));
        this.add(new JLabel("月"));
        this.add(this.comboBoxDay=new JComboBox<Integer>());
        this.add(new JLabel("日"));

        this.comboBoxYear.addActionListener(this);
        this.comboBoxMonth.addActionListener(this);
        this.comboBoxDay.addActionListener(this);

        this.comboBoxYear.setEditable(false);
        this.comboBoxMonth.setEditable(false);
        this.comboBoxDay.setEditable(false);

        this.comboBoxYear.setSelectedItem(date.getYear());
        this.comboBoxMonth.setSelectedItem(date.getMonth());
        this.comboBoxDay.setSelectedItem(date.getDay());


    }

    public void setEnabled(boolean f)
    {
        this.comboBoxYear.setEnabled(f);
        this.comboBoxMonth.setEnabled(f);
        this.comboBoxDay.setEnabled(f);
    }

    public void setActionListener(ActionListener updater)
    {
        this.listener=updater;
        this.listener.actionPerformed(new ActionEvent(this,1,"update"));
    }

    public MyDate getMyDate()
    {
        Object y=this.comboBoxYear.getSelectedItem();
        Object m=this.comboBoxMonth.getSelectedItem();
        Object d=this.comboBoxDay.getSelectedItem();
        if(y instanceof Integer&&m instanceof Integer&&d instanceof Integer)
        {
            return new MyDate((int)y,(int)m,(int)d);
        }
        return new MyDate();
    }

    public Date getDate()
    {
        return new Date();
    }

    public void set(MyDate mydate)
    {
        System.out.println(mydate);
        this.comboBoxYear.setSelectedItem(mydate.getYear());
        this.comboBoxMonth.setSelectedItem(mydate.getMonth());
        this.comboBoxDay.setSelectedItem(mydate.getDay());
    }

    public void actionPerformed(ActionEvent e)
    {
        if((e.getSource().equals(this.comboBoxYear)||e.getSource().equals(this.comboBoxMonth))&&this.comboBoxYear.getSelectedItem()!=null&&this.comboBoxMonth.getSelectedItem()!=null)
        {
            int dom=MyDate.daysOfMonth((int)this.comboBoxYear.getSelectedItem(),(int)this.comboBoxMonth.getSelectedItem());
//            System.out.println("dom:"+dom);
            if(this.comboBoxDay.getItemCount()!=dom)
            {
                //只有在换的年或者月导致天数变化的时候才重新绘制
                Integer day=1;
                if(this.comboBoxDay.getSelectedItem()!=null)
                    day=(Integer)this.comboBoxDay.getSelectedItem();
                this.comboBoxDay.removeAllItems();
                for(int i=0;i<dom;++i,this.comboBoxDay.addItem(i)) ;
                if(day<=dom)
                    this.comboBoxDay.setSelectedItem(day);
                else
                    this.comboBoxDay.setSelectedItem(1);
            }
        }
        if(this.listener!=null)
        {
            this.listener.actionPerformed(new ActionEvent(this,1,"update"));
        }
    }
}
